%no : nomor soal berdasarkan soal tugas
%A  : matriks data
function [hasil] = mn_lagrange(A, titik_interpolasi, derajat, handles)
%UNTITLED3 Summary of this function goes here
%   Detailed explanation goes here
if (length(A(1,:))==2)          %interpolasi 1 peubah
    X = A(:,1);
    A;
    Y = A(:,2);
    disp(X);
    disp(Y);
    D = length (X);
    ST = 0;
    for j = 1 : D
    ST (j) = X(j,1) - titik_interpolasi;
    end
    T = 0;
    for j = 1 : D - 1
        if (ST(j)*ST(j+1) <= 0)
            if(abs(ST(j)) > abs (ST(j+1)) )
                T = j + 1;
            else
                T = j;
            end
        end
    end
    start = 0;
    if (T == 0)
        start = D - derajat + 1;
    else 
        if (D - T < derajat / 2 )
            start = D - derajat + 1;
        else
            if (T <= derajat / 2 )
                start = 1;
            else
                if (mod(derajat,2) == 0)
                    start = T - (derajat/2) + 1;
                else
                    start = T - (derajat + 1) / 2 + 1;
                end
            end
        end
    end
    kali = 0;
    total = 0;
    start;
    derajat;
    T;
    D;
    set(handles.uitable_selisih,'Data',ST);
    
    for xx=A(1,1):A(length(A(:,1)),1)
        total = 0;
        kali = 0;
        jumlah = ST(1,1);
        for j = start : (start + derajat - 1)
            perkalian = 1;
            for k = start : start + derajat - 1
                if (j ~= k) perkalian = perkalian * (xx - X(k)) / (X(j) - X(k));
                end
            end
            kali(j) = perkalian;
            total = total + (Y(j) * perkalian);
        end
        P(xx-A(1,1)+1,1) = xx;
        P(xx-A(1,1)+1,2) = total;
    end
    axis(handles.axes1);
    cla(handles.axes1,'reset');
    plot(P(:,1),P(:,2),'b');
    hold on;
    plot(A(:,1),A(:,2),'r*');
    
    kali = 0;
    total = 0;
    for j = start : (start + derajat - 1)
        perkalian = 1;
        for k = start : start + derajat - 1
            if (j ~= k) perkalian = perkalian * (titik_interpolasi - X(k)) / (X(j) - X(k));
            end
        end
        kali(j) = perkalian;
            total = total + (Y(j) * perkalian);
    end
    hasil = total;
elseif (length(A(1,:))==3)      %interpolasi 2 peubah
    titik_interpolasi = [15 50];
    X = A(:,1);
    Y = A(:,2);
    Z = A(:,3);
    D = length(X);
    XL = X(1);
    Table1 = 0;
    Table2 = 0;
    k = 1;
    l = 1;
    for i = 1:D
        if (XL ~= X(i))
            Table1
            Table2(k,1)= XL;
            Table2(k,2)= mn_lagrange(Table1,titik_interpolasi(1,2),derajat, handles);
            XL = X(i);
            l = 1;
            k = k +1;
            Table1 = 0;
        end
        Table1(l,1) = Y(i);
        Table1(l,2) = Z(i);
        l = l + 1;
    end
    Table2
    Table2(k,1)= XL;
    Table2(k,2)= mn_lagrange(Table1,titik_interpolasi(1,2),derajat, handles);
    Table2;
    hasil = mn_lagrange(Table2,titik_interpolasi(1,1),derajat, handles);
end


